public class Demo4 {
    public static void main(String[] args) throws InterruptedException {
        Thread[] threads = new Thread[20];
        for(int i=0; i<20; i++){
            final int n = i;
            threads[i] = new Thread(new Runnable() {
                @Override
                public void run() {//内部类使用外部的变量，必须是final修饰
                    System.out.println(n);
                }
            });
        }
        for(Thread t : threads){
            t.start();
        }
        for(Thread t : threads){//同时执行20个线程，再等待所有线程执行完毕
            t.join();
        }
        System.out.println("OK");
    }
    public static void main1(String[] args) throws InterruptedException {
        Thread t1 = new Thread(() -> {
                System.out.println("0");
        });
        Thread t2 = new Thread(() -> {
            try {
                t1.join();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println("1");
        });
        Thread t3 = new Thread(() -> {
            try {
                t2.join();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println("2");
        });
        Thread t4 = new Thread(() -> {
            try {
                t3.join();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println("3");
        });
        Thread t5 = new Thread(() -> {
            try {
                t4.join();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println("4");

        });
        Thread t6 = new Thread(() -> {
            try {
                t5.join();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println("5");

        });
        t1.start();
        t2.start();
        t3.start();
        t4.start();
        t5.start();
        t6.start();
        t1.join();
        t2.join();
        t3.join();
        t4.join();
        t5.join();
        t6.join();
        System.out.println("ok");
    }
}
